<template>
  <el-form label-width="100px">
    <el-form-item label="SKU名称">
      <el-input placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
    </el-form-item>
    <el-form-item label="价格(元)">
      <el-input type="number" placeholder="价格(元)" v-model="skuParams.price"></el-input>
    </el-form-item>
    <el-form-item label="重量(g)">
      <el-input type="number" placeholder="重量(g)" v-model="skuParams.weight"></el-input>
    </el-form-item>
    <el-form-item label="SKU描述">
      <el-input type="textarea" placeholder="SKU描述" v-model="skuParams.skuDesc"></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form inline>
        <el-form-item v-for="attr in attrArr" :key="attr.id" :label="attr.attrName">
          <el-select style="width: 200px;" v-model="attr.attrIdAndValueId">
            <el-option v-for="attrValue in attr.attrValueList" :key="attrValue.id" :label="attrValue.valueName"
                       :value="`${attr.id}:${attrValue.id}`"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="销售属性">
      <el-form inline>
        <el-form-item v-for="sale in saleArr" :key="sale.id" :label="sale.saleAttrName">
          <el-select style="width: 200px;" v-model="sale.saleIdAndValueId">
            <el-option v-for="saleAttrValue in sale.spuSaleAttrValueList" :key="saleAttrValue.id"
                       :label="saleAttrValue.saleAttrValueName" :value="`${sale.id}:${saleAttrValue.id}`"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table border :data="imgArr" ref="table">
        <el-table-column type="selection" width="80px" align="center"></el-table-column>
        <el-table-column label="图片">
          <template #="{ row }">
            <img :src="row.imgUrl" style="width: 100px;height: 100px;">
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName"></el-table-column>
        <el-table-column label="操作">
          <template #="{ row }">
            <el-button type="primary" size="default" @click="setImgDefault(row)">设置默认</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="default" @click="save">保存</el-button>
      <el-button size="default" @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { reqAttr } from '@/api/product/attr';
import { reqAddSku, reqSpuHasSaleAttr, reqSpuImageList } from '@/api/product/spu';
import type { SPUData, SkuData } from '@/api/product/spu/type';
import { ElMessage } from 'element-plus';
import { reactive, ref } from 'vue';

const $emit = defineEmits(["changeScene"]);

const attrArr = ref<any>([])
const saleArr = ref<any>([])
const imgArr = ref<any>([])
const table = ref<any>()

// 收集 sku 数据
const skuParams = reactive<SkuData>({
  category3Id: "",
  spuId: "",
  tmId: "",

  skuName: "",
  price: "",
  weight: "",
  skuDesc: "",
  skuAttrValueList: [],
  skuSaleAttrValueList: [],
  skuDefaultImg: "",
})

// 获取添加SKU的初始数据
const initSkuData = async (c1Id: number, c2Id: number, spu: SPUData) => {
  skuParams.category3Id = spu.category3Id;
  skuParams.spuId = spu.id as number;
  skuParams.tmId = spu.tmId;
  // 获取平台属性、销售属性、图片数据
  const result1: any = await reqAttr(c1Id, c2Id, spu.category3Id)
  const result2: any = await reqSpuHasSaleAttr(spu.id as number)
  const result3: any = await reqSpuImageList(spu.id as number)
  attrArr.value = result1.data;
  saleArr.value = result2.data;
  imgArr.value = result3.data;
}

// 设置默认图片的回调
const setImgDefault = (row: any) => {
  imgArr.value.forEach((item: any) => {
    table.value.toggleRowSelection(item, false);
  });
  table.value.toggleRowSelection(row, true);
  skuParams.skuDefaultImg = row.imgUrl
}

// 保存按钮的回调
const save = async () => {
  skuParams.skuAttrValueList = attrArr.value.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      const [attrId, valueId] = next.attrIdAndValueId.split(":")
      prev.push({
        attrId: attrId,
        valueId: valueId
      })
    }
    return prev
  }, [])
  skuParams.skuSaleAttrValueList = saleArr.value.reduce((prev: any, next: any) => {
    if (next.saleIdAndValueId) {
      const [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(":")
      prev.push({
        saleAttrId: saleAttrId,
        saleAttrValueId: saleAttrValueId
      })
    }
    return prev
  }, [])
  const result: any = await reqAddSku(skuParams)
  if (result.code === 200) {
    ElMessage({
      type: 'success',
      message: '添加SKU成功'
    })
    $emit("changeScene", { flag: 0, params: "" });
  } else {
    ElMessage({
      type: 'error',
      message: '添加SKU失败'
    })
  }
}

// 取消按钮的回调
const cancel = () => {
  $emit("changeScene", { flag: 0, params: "" });
};

defineExpose({
  initSkuData
})
</script>

<style scoped></style>
